/**
 * 
 */
$(function() {
	$("#nameId").val(-1);
	var ops = $("#address");// 下拉列表

	$.ajax({
		type : "POST",
		url : "addIncome.do?method=0",
		dataType : "json",
		async : false,
		success : function(data) {
			var depts = data.depts;
			appendOps(depts);
			ops.change(function() {
				fillForm(depts, $(this).val());
			});
		}
	});
	function appendOps(depts) {
		for (var i = 0; i < depts.length; i++) {
			ops.append("<option value='" + i + "'>" + i + ":"
					+ depts[i].departmentName.substr(0, 6) + "...</option>");
		}
	}

	// 填充表单
	function fillForm(depts, index) {

		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = year + seperator1 + month + seperator1 + strDate;

		if (index == -1) {
			$("#deptName").val('');
			$("#daily").val('');
			$("#bussiness_date").val('');
			$("#last_mod_date").val('');
			$("#addressId").val(-1);
			return;
		}
		$("#deptName").val(depts[index].departmentName);
		$("#bussiness_date").val(currentdate);
		$("#last_mod_date").val(currentdate);
		$("#nameId").val(depts[index].id);
	}
	// 获取当前时间，格式YYYY-MM-DD
	function getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = year + seperator1 + month + seperator1 + strDate;
		return currentdate;
	}
});